#!/usr/bin/env python
# encoding: utf-8
'''
@author: wangjianrong
@software: pycharm
@file: save_target_labels.py
@time: 2020/9/22 10:25
@desc:将原始标签保存成新的标签，如只保存road的标签，则7->0，其他类别全部改成255
'''

import cv2
from opencv_op.read_img import cv_imread,cv_imwrite
import os
import numpy as np
from common.path_op import makedirs

target_ids = [7]
new_labels = list(range(len(target_ids)+1))

root_folder = 'G:/下载文件/数据/cityscapes/'
label_folder = root_folder + 'gtFine_val/val/frankfurt/'
dst_folder = root_folder + 'labels/'

makedirs(dst_folder,delete=True)
list_label_imgs = [filename for filename in os.listdir(label_folder) if 'labelIds' in filename]

target_id = 0
for filename in list_label_imgs:
    new_filename = filename.split('_')[:3] + ['leftImg8bit']
    new_filename = '_'.join(new_filename) + '.png'
    dst_path = dst_folder + new_filename
    img = cv_imread(label_folder + filename,-1)
    new_img = np.ones_like(img) * 255
    for target_id in target_ids:
        new_id = target_ids.index(target_id)
        inds = img == target_id
        new_img[inds] = new_id
    cv_imwrite(dst_path,new_img)
    print(np.unique(img))
    print(np.unique(new_img))





